home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / faq / wdj0597.zip / TECHTIPS.ZIP / REORDER.C < prev   
C/C++ Source or Header  |  1997-02-21  |  2KB  |  67 lines

  1. #include <windows.h>
  2. #include <windowsx.h>
  3.  
  4. static WNDPROC  oldListProc;
  5.  
  6. LRESULT CALLBACK _export DragListProc(HWND hwnd,
  7.             UINT uMsg, WPARAM wParam, LPARAM lParam)
  8. {
  9.     static int  anchorIndex;
  10.     LRESULT     lResult;
  11.  
  12.     lResult = CallWindowProc((FARPROC) oldListProc, hwnd,
  13.                     uMsg, wParam, lParam);
  14.  
  15.     switch(uMsg) {
  16.         case WM_LBUTTONDOWN:
  17.             if (GetCapture() == hwnd)
  18.                 anchorIndex = ListBox_GetCurSel(hwnd);
  19.             else
  20.                 anchorIndex = -1;
  21.             break;
  22.         case WM_LBUTTONUP:
  23.             if (anchorIndex >= 0) {
  24.                 MoveSelection(hwnd, anchorIndex);
  25.                 anchorIndex = -1;
  26.             }
  27.             break;
  28.         case WM_MOUSEMOVE:
  29.         case 0x0118:            /* undocumented: WM_SYSTIMER */
  30.             if (anchorIndex >= 0 && GetCapture() == hwnd)
  31.                 anchorIndex = MoveSelection(hwnd, anchorIndex);
  32.             break;
  33.     }
  34.  
  35.     return(lResult);
  36. }
  37.  
  38.  
  39. /*
  40. **  MoveSelection()
  41. **  Compares the current selection with the anchor selection.  If
  42. **  different, moves the anchor selection to the new position.
  43. */
  44. static int MoveSelection(HWND hwnd, int anchorIndex)
  45. {
  46.     DWORD   itemData;
  47.     char    copyString[255];
  48.     int newIndex;
  49.  
  50.     newIndex = ListBox_GetCurSel(hwnd);
  51.     if (newIndex >= 0 && newIndex != anchorIndex) {
  52.         SetWindowRedraw(hwnd, FALSE);
  53.         (void)ListBox_GetText(hwnd, anchorIndex, copyString);
  54.         itemData = ListBox_GetItemData(hwnd, anchorIndex);
  55.         (void)ListBox_DeleteString(hwnd, anchorIndex);
  56.  
  57.         (void)ListBox_InsertString(hwnd, newIndex, copyString);
  58.         (void)ListBox_SetItemData(hwnd, newIndex, itemData);
  59.         (void)ListBox_SetCurSel(hwnd, newIndex);
  60.         SetWindowRedraw(hwnd, TRUE);
  61.     } else
  62.         newIndex = anchorIndex;
  63.  
  64.     return(newIndex);
  65. }
  66.  
  67.